package com.bigmodel.controller;

import cn.hutool.json.JSONUtil;
import com.bigmodel.baidu.BaiduWenXin;
import com.bigmodel.baidu.MarkdownUtil;
import com.bigmodel.taskmanager.TaskService;
import com.bigmodel.util.FtlUtil;
import com.bigmodel.util.RequestDTO;
import com.bigmodel.util.ResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 应急预案演练
 */
@RestController
@RequestMapping("/emergency")
public class EmergencyPlanController {
    /** 应急预案 */
    static String emergencyPlan;

    @Autowired
    private TaskService taskService;

    /**
     * 上传应急预案
     * @param request
     * @return
     */
    @PostMapping("/uploadEmergencyPlan")
    public String uploadEmergencyPlan(@RequestBody RequestDTO request) {
        ResultDTO result = new ResultDTO();
        emergencyPlan = request.getMessage();
        result.setResultCode("ok");
        result.setResultMessage("上传成功");
        return  JSONUtil.toJsonStr(result);
    }



    /**
     * 故障处置
     */
    @PostMapping("/faultHandlingV2")
    public String faultHandlingV2(@RequestBody RequestDTO request) {
        ResultDTO result = new ResultDTO();
        //调用大模型拼装任务拆分
        Map<String,Object> map = new HashMap<>();
        map.put("faultContent",request.getMessage());
        String questionContent = FtlUtil.generateContent(map, "api/solution.ftl");

        List<Map<String,String>> messages = new ArrayList<>();
        {
            Map<String, String> messageItem = new HashMap<>();
            messageItem.put("role", "user");
            messageItem.put("content", questionContent);
            messages.add(messageItem);
        }
        String taskPlan = BaiduWenXin.chatList(messages);
        System.out.println(taskPlan);

        String taskJson = MarkdownUtil.extractJsonText(taskPlan);
        List<String> answereList = new ArrayList<>();
        taskService.execute(taskJson,answereList);

        result.setResultCode("ok");
        result.setResultMessage(emergencyPlan);
        return  JSONUtil.toJsonStr(result);
    }



}
